package com.imageim.yimirror.common.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

import com.imageim.yimirror.R;

/**
 * Created by Bruce on 2017/8/7.
 */

public class SoundUtil {
    private static MediaPlayer mediaPlayer;
    private static SoundPool soundPool;

    public static int SOUNDID_001;
    public static int SOUNDID_002;
    public static int SOUNDID_003;
    public static int SOUNDID_004;
    public static int SOUNDID_005;
    public static int SOUNDID_006;
    public static int SOUNDID_007;
    public static int SOUNDID_008;
    public static int SOUNDID_009;
    public static int SOUNDID_010;
    public static int SOUNDID_SHOT_EFFECT;

    public static int STREAMID_001;
    public static int currStreamId;

    public static void loadAll(Context context) {
        if (soundPool != null) {
            return;
        }
        soundPool = new SoundPool(12, AudioManager.STREAM_MUSIC, 1);
        SOUNDID_001 = soundPool.load(context, R.raw.a001, 1);
        SOUNDID_002 = soundPool.load(context, R.raw.a002, 1);
        SOUNDID_003 = soundPool.load(context, R.raw.a003, 1);
        SOUNDID_004 = soundPool.load(context, R.raw.a004, 1);
        SOUNDID_005 = soundPool.load(context, R.raw.a005, 1);
        SOUNDID_006 = soundPool.load(context, R.raw.a006, 1);
        SOUNDID_007 = soundPool.load(context, R.raw.a007, 1);
        SOUNDID_008 = soundPool.load(context, R.raw.a008, 1);
        SOUNDID_009 = soundPool.load(context, R.raw.a009, 1);
        SOUNDID_010 = soundPool.load(context, R.raw.a010, 1);
        //        SOUNDID_SHOT_EFFECT = soundPool.load(context, R.raw.shot_effect, 1);
    }

    public static int play(int audioResId) {
        int soundId = 0;
        switch (audioResId) {
            case R.raw.a001:
                soundId = SOUNDID_001;
                break;
            case R.raw.a002:
                soundId = SOUNDID_002;
                break;
            case R.raw.a003:
                soundId = SOUNDID_003;
                break;
            case R.raw.a004:
                soundId = SOUNDID_004;
                break;
            case R.raw.a005:
                soundId = SOUNDID_005;
                break;
            case R.raw.a006:
                soundId = SOUNDID_006;
                break;
            case R.raw.a007:
                soundId = SOUNDID_007;
                break;
            case R.raw.a008:
                soundId = SOUNDID_008;
                break;
            case R.raw.a009:
                soundId = SOUNDID_009;
                break;
            case R.raw.a010:
                soundId = SOUNDID_010;
                break;
            case R.raw.shot_effect:
                soundId = SOUNDID_SHOT_EFFECT;
                break;
            default:
        }
        currStreamId = soundPool.play(soundId, 1, 1, 0, 0, 1);
        return currStreamId;
    }

    public static void stop(int streamId) {
        if (soundPool != null) {
            soundPool.stop(streamId);
        }
    }

    public static void playAsyc(Context context, int audioResId, boolean loop) {
        if (context != null) {
            final int soundId = soundPool.load(context, audioResId, 1);
            soundPool.setOnLoadCompleteListener((soundPool, sampleId, status) ->
                    soundPool.play(soundId, 1, 1, 0, loop ? 1 : 0, 1)
            );
        }
    }

    public static void playWithMediaPlayer(Context context, int audioResId) {
        stop();
        mediaPlayer = MediaPlayer.create(context, audioResId);
        mediaPlayer.setLooping(false);
        mediaPlayer.start();
    }

    public static void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

}
